home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / iDrag.0.6 / IconView.m < prev    next >
Text File  |  1995-06-12  |  6KB  |  198 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "IconView.h"
  5.  
  6. @implementation IconView
  7.  
  8. /* appDidInit is the first message sent after all objects, connections, and
  9.    IDs have been initialized.  This method simply orders the window to the
  10.    front and then registers the window with speaker/listener so it can
  11.    receive icon related events.  The appDidInit message is only sent to this
  12.    object because the File's Owner's delegate is connected to this
  13.    viewObject within IB.
  14. */
  15.  
  16.   - appDidInit: sender
  17. {
  18.    [self registerWindow];
  19.     
  20.    return self;
  21. }
  22.  
  23. /* basic view newFrame method-- nothing special here.  Just messages to self
  24.    to initialize the various variables.
  25. */
  26.  
  27. + newFrame: (NXRect *) theRect
  28. {
  29.    self = [super newFrame:theRect];
  30.    
  31.    return self;
  32. }
  33.  
  34.  
  35. /* drawSelf would normally update the view with whatever the application would
  36.    display there.  In this case, it simply erases the view and draws a string
  37.    art design so the window isn't completely boring.
  38. */
  39. - drawSelf:(const NXRect *)rects :(int)rectCount
  40. {
  41.   int i;
  42.     
  43.   PSsetgray(0.333);
  44.   NXRectFill(&bounds);
  45.  
  46.   PSsetgray(1.0);
  47.   PSnewpath();
  48.   for(i=0; i<100; i+=4) {
  49.     PSmoveto(100-i,100);
  50.     PSlineto(100, i);
  51.     PSlineto(100+i, 100);
  52.     PSlineto(100, 200-i);
  53.     PSlineto(100-i, 100);
  54.   }
  55.   PSclosepath();
  56.   PSstroke();
  57.  
  58.   return self;
  59. }
  60.  
  61. /* register the window with the Workspace Manager so that icon messages
  62.    will be sent this way as neccessary. First grab the application's
  63.    speaker, then get a new Listener.  Once the listener is set up, the
  64.    global window number is registered with the workspace manager.*/
  65. - registerWindow
  66. {
  67.     unsigned int windowNum;
  68.     id speaker = [NXApp appSpeaker];
  69.     
  70.     listener = [Listener new];
  71.     if(!speaker || !listener) { /* if we failed to get the application's
  72.                    speaker or an instance of a listener,
  73.                    then warn user and die */
  74.       NXRunAlertPanel("Application Death Warrant",
  75.               "Could not create new Speaker or Listener Object in
  76.  registerWindow method.", "Quit", NULL, NULL);
  77.       exit(1);
  78.     }
  79.     [listener setDelegate:self]; /* set listener's delegate to this view. */
  80.     [listener privatePort]; /* This causes the listener to not be registered
  81.                    publically-- no application can message to
  82.                    this listener without specifically sending
  83.                    the rights to do so */
  84.     [listener addPort]; /* Adds the port to the list of ports that can
  85.                receive methods asynchronously */
  86.     NXConvertWinNumToGlobal([window windowNum], &windowNum);
  87.     [speaker setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
  88.     [speaker registerWindow:windowNum toPort:[listener listenPort]];
  89.  
  90.     return self;
  91. }
  92.  
  93. /* unregister the window and free the listener object */
  94. - unregisterWindow
  95. {
  96.     unsigned int windowNum;
  97.     id speaker = [NXApp appSpeaker];
  98.  
  99.     if (listener) {
  100.     [speaker setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
  101.     NXConvertWinNumToGlobal([window windowNum], &windowNum);
  102.     [speaker unregisterWindow:windowNum];
  103.     [listener free];
  104.     }
  105.  
  106.     return self;
  107. }
  108.  
  109. /* the following four methods deal with the various icon events.  All of
  110.    these methods come from the listener that was set up and registered
  111.    with the WorkSpace Manager in the -registerWindow method.  At the moment,
  112.    nothing useful is done with the information passed back other than
  113.    dumping the data to the various fields in the info window...
  114.  
  115.    Only the methods you are going to use need be implemented (except for the
  116.    caveat mentioned below).
  117.  
  118.    Caveat:  If you are going to do anything useful with the file at the other
  119.    end of the icon, you must trap for the -iconEntered message and store the
  120.    pathList somewhere (instance variable, perhaps?)... even if you only care
  121.    about the -iconReleasedAt stuff.
  122.    */
  123.  
  124. /* handle iconEntered-- iconEntered is sent when an icon first
  125.    enters the target area. */
  126. - (int)iconEntered:(int)windowNum at:(double)x :(double)y
  127.     iconWindow:(int)iconWindowNum iconX:(double)iconX iconY:(double)iconY
  128.     iconWidth:(double)iconWidth iconHeight:(double)iconHeight
  129.     pathList:(char *)pathList
  130. {
  131.   char s[100];
  132.  
  133.   [pathField setStringValue: pathList];
  134.   sprintf(s, "%d", windowNum);
  135.   [windowNumField setStringValue:s];
  136.   sprintf(s, "%d", iconWindowNum);
  137.   [iconWindowField setStringValue:s];
  138.   sprintf(s, "%.1f", iconWidth);
  139.   [iconWidthField setStringValue:s];
  140.   sprintf(s, "%.1f", iconHeight);
  141.   [iconHeightField setStringValue:s];
  142.   sprintf(s, "(%.1f, %.1f)\n", x, y);
  143.   [windowCoordField setStringValue:s];
  144.   sprintf(s, "(%.1f, %.1f)\n", iconX, iconY);
  145.   [iconCoordField setStringValue:s];
  146.  
  147.   return 0;
  148. }
  149.  
  150. /* handle iconReleasedAt-- iconReleased is sent with the coordinates of
  151.    where the icon was dropped within the target area. */
  152. - (int)iconReleasedAt:(double)x :(double)y ok:(int *)flag
  153. {
  154.   char s[100];
  155.   
  156.   sprintf(s, "(%.1f, %.1f)\n", x, y);
  157.   [iconReleasedField setStringValue:s];
  158.   *flag = 0; /* since we don't do anything, have the Workspace animate
  159.         or Icon back to it's source */
  160.   return 0;
  161. }
  162.  
  163. /* handle iconExitedAt-- iconExitedAt is sent when the icon leaves the
  164.    targeted area */
  165. - (int)iconExitedAt:(double)x :(double)y
  166. {
  167.   char s[100];
  168.   
  169.   sprintf(s, "(%.1f, %.1f)\n", x, y);
  170.   [iconExitedField setStringValue:s];
  171.   return 0;
  172. }
  173.  
  174. /* handle iconMovedTo-- within the target area, this message is sent every
  175.    time the icon moves. */
  176. - (int)iconMovedTo:(double)x :(double)y
  177. {
  178.   char s[100];
  179.   
  180.   sprintf(s, "(%.1f, %.1f)\n", x, y);
  181.   [iconMovedField setStringValue:s];
  182.   return 0;
  183. }
  184.  
  185. /* IB methods to set the various field ID's */
  186.  
  187. - setPathField:anObject { pathField = anObject; return self; }
  188. - setWindowNumField:anObject { windowNumField = anObject; return self; }
  189. - setIconWindowField:anObject { iconWindowField = anObject; return self;}
  190. - setIconHeightField:anObject {  iconHeightField = anObject; return self;}
  191. - setIconWidthField:anObject {  iconWidthField = anObject; return self;}
  192. - setWindowCoordField:anObject { windowCoordField = anObject; return self;}
  193. - setIconCoordField:anObject { iconCoordField = anObject; return self;}
  194. - setIconReleasedField:anObject { iconReleasedField = anObject; return self;}
  195. - setIconExitedField:anObject {  iconExitedField = anObject; return self;}
  196. - setIconMovedField:anObject {  iconMovedField = anObject; return self;}
  197. @end
  198.